﻿Imports System.IO
Imports System.Text.RegularExpressions
Imports System.Net
Imports System.Net.Sockets


Public Class InputForm
    Public Divided As Array
    Public BarcodeString As String
    Public sendString As String
    'Private _Connection As ConnectionInfo
    'Private _ServerAddress As IPAddress
    Private TextBoxArray As TextBox()
    Private states_of_towns As String()
    Private towns As String()
    Dim day, month, year As String
    Dim months As String() = {"january", "february", "march", "april", "may", "june", "july", "august", "september", "october", "november", "december", "jan", "feb", "mar", "apr", "may", "jun", "jul", "aug", "sep", "oct", "nov", "dec", "sept"}
    Dim monthsNumeric As String() = {"01", "02", "03", "04", "05", "06", "07", "08", "09", "10", "11", "12", "01", "02", "03", "04", "05", "06", "07", "08", "09", "10", "11", "12", "09"}
    Dim days As String() = {"1st", "2nd", "3rd", "4th", "5th", "6th", "7th", "8th", "9th", "10th", "11th", "12th", "13th", "14th", "15th", "16th", "17th", "18th", "19th", "20th", "21st", "22nd", "23rd", "24th", "25th", "26th", "27th", "28th", "29th", "30th", "31st", "first", "second", "third", "fourth", "fifth", "sixth", "seventh", "eigth", "ninth", "tenth", "eleventh", "twelfth", "thirteenth", "fourteenth", "fifthteenth", "sixteenth", "seventeenth", "eighteenth", "nineteenth", "twentieth"}
    Dim daysNumeric As String() = {"01", "02", "03", "04", "05", "06", "07", "08", "09", "10", "11", "12", "13", "14", "15", "16", "17", "18", "19", "20", "21", "22", "23", "24", "25", "26", "27", "28", "29", "30", "31", "01", "02", "03", "04", "05", "06", "07", "08", "09", "10", "11", "12", "13", "14", "15", "16", "17", "18", "19", "20"}

    Private Sub InputForm_Load(sender As Object, e As EventArgs) Handles MyBase.Load
        'Clears out picture folder
        Dim counter As  _
        System.Collections.ObjectModel.ReadOnlyCollection(Of String)
        counter = My.Computer.FileSystem.GetFiles("C:\test\HerbariumPictures")
        If counter.Count > 20 Then
            Dim s As String
            For Each s In System.IO.Directory.GetFiles("C:\test\HerbariumPictures")
                System.IO.File.Delete(s)
            Next s
            My.Computer.FileSystem.CopyFile( _
    "C:\test\EmptyandFolderPics\White.jpg", _
    "C:\test\HerbariumPictures\White.jpg")
        End If

        TextBoxArray = {m1p, m2v, ScientificName, Genus, Species, Ir, i, Author, cc, RecordEnteredBy, Barcode, State, County, Town, Collector, Qualifier, VerbatimCollectionDate}
        RecordEnteredBy.Text = Testing1.Login.LoginTextBox.Text

        'Validate children
        ValidateChildren()

        Login.ProgressBar1.Increment(33)
        'Load states, counties, and cities in autocomplete custom source 
        ' Create a string collection for states
        Dim states As New AutoCompleteStringCollection
        states.Add("Connecticut")
        states.Add("Maine")
        states.Add("Massachusetts")
        states.Add("New Hampshire")
        states.Add("Rhode Island")
        states.Add("Vermont")
        State.AutoCompleteMode = AutoCompleteMode.SuggestAppend
        State.AutoCompleteSource = AutoCompleteSource.CustomSource
        State.AutoCompleteCustomSource = states

        Login.ProgressBar1.Increment(33)
        'Create a string collection for counties
        Dim countiesFile As String = "C:\test\Counties_20121114.txt"
        If System.IO.File.Exists(countiesFile) = True Then
            'Read the textfile:
            Dim objReader As New System.IO.StreamReader(countiesFile)
            Dim MyFileLine As String = objReader.ReadToEnd
            objReader.Close()

            Dim Arg() As String = Split(MyFileLine, ",")    'Split file contents at ,
            Dim Counties As New AutoCompleteStringCollection
            County.AutoCompleteMode = AutoCompleteMode.SuggestAppend
            County.AutoCompleteSource = AutoCompleteSource.CustomSource
            County.AutoCompleteCustomSource = Counties
            For index = 0 To Arg.Length - 1
                Counties.Add(Arg(index))
            Next
        Else
            MessageBox.Show("File 'C:\test.txt' not found!", "www.interloper.nl", MessageBoxButtons.OK, MessageBoxIcon.Error)
        End If

        Login.ProgressBar1.Increment(33)
        'Create a string collection for cities
        Dim citiesFile As String = "C:\test\cities_20121114.txt"
        If System.IO.File.Exists(citiesFile) = True Then
            'Read the textfile:
            Dim objReader As New System.IO.StreamReader(citiesFile)
            Dim MyFileLine As String = objReader.ReadToEnd
            objReader.Close()
            MyFileLine = MyFileLine.Replace(vbCr, ",").Replace(vbLf, ",")
            'MyFileLine = Regex.Replace(MyFileLine, "[0-9]", "")
            'MyFileLine = Replace(MyFileLine, "Connecticut", "")
            'MyFileLine = Replace(MyFileLine, "Maine", "")
            'MyFileLine = Replace(MyFileLine, "Massachusetts", "")
            'MyFileLine = Replace(MyFileLine, "New Hampshire", "")
            'MyFileLine = Replace(MyFileLine, "Rhode Island", "")
            'MyFileLine = Replace(MyFileLine, "Vermont", "")
            'MyFileLine = Replace(MyFileLine, ",,", "")
            'MessageBox.Show(MyFileLine)
            Dim subStrings() As String = Regex.Split(MyFileLine, ",")
            ReDim towns((subStrings.Length - 1) / 3 - 2)
            For i As Integer = 3 To subStrings.Length - 2
                If i Mod 3 = 0 Then
                    towns(i / 3 - 1) = subStrings(i)
                End If
            Next
            ReDim states_of_towns((subStrings.Length - 1) / 3 - 2)
            For i As Integer = 3 To subStrings.Length - 2
                If i Mod 3 = 0 Then
                    states_of_towns(i / 3 - 1) = subStrings(i + 2)
                End If
            Next

            Dim Cities As New AutoCompleteStringCollection
            Town.AutoCompleteMode = AutoCompleteMode.SuggestAppend
            Town.AutoCompleteSource = AutoCompleteSource.CustomSource
            Town.AutoCompleteCustomSource = Cities
            Cities.AddRange(towns)
        Else
            MessageBox.Show("File 'C:\Users\TouchScreen\Documents\Lists\FW__Apparatus_database_and_UI\cities_20121114.txt' not found!", "File not found", MessageBoxButtons.OK, MessageBoxIcon.Error)
        End If

        ScientificName.Select()

    End Sub

    Private Sub Button1_Click(sender As Object, e As EventArgs) Handles sendButton.Click
        'Moves cursor to barcode textbox
        Barcode.Focus()
        'Sends the information to the .txt file
        sendString = ""
        sendString = sendString & "{"
        sendString = sendString & """m1p"":""" & m1p.Text & ""","
        sendString = sendString & """m2v"":""" & m2v.Text & ""","
        sendString = sendString & """sn"":""" & ScientificName.Text & ""","
        sendString = sendString & """g"":""" & Genus.Text & ""","
        sendString = sendString & """s"":""" & Species.Text & ""","
        sendString = sendString & """ir"":""" & Ir.Text & ""","
        sendString = sendString & """i"":""" & i.Text & ""","
        sendString = sendString & """a"":""" & Author.Text & ""","
        sendString = sendString & """st"":""" & State.Text & ""","
        sendString = sendString & """ct"":""" & County.Text & ""","
        sendString = sendString & """cl"":""" & Collector.Text & ""","
        sendString = sendString & """cc"":""" & cc.Text & ""","
        sendString = sendString & """RecordEnteredBy"":""" & RecordEnteredBy.Text & ""","
        sendString = sendString & """Town"":""" & Town.Text & ""","
        sendString = sendString & """IDqual"":""" & Qualifier.Text & ""","
        sendString = sendString & """BeginDate"":""" & dateLabel.Text & ""","
        sendString = sendString & """EndDate"":""" & EndingDate.Text & ""","
        sendString = sendString & """Verbatim"":""" & VerbatimCollectionDate.Text & ""","
        sendString = sendString & """Barcode"":""" & Barcode.Text & ""","

        'If MissingInformation checked then send 1
        sendString = sendString & """MI"":""" & MissingInformation.CheckState & ""","
        'If IllegibleInformation.Checked Then send 1
        sendString = sendString & """IllInfo"":""" & IllegibleInformation.CheckState & ""","
        'If Unreadable.Checked Then send 1
        sendString = sendString & """UR"":""" & Unreadable.CheckState & ""","
        'If UncertainDateCheckBox.Checked Then send 1
        sendString = sendString & """UD"":""" & UncertainDateCheckBox.CheckState & ""","
        'If Empty.Checked Then send 1
        sendString = sendString & """Emp"":""" & Empty.CheckState & ""","
        'If Folder.Checked Then send 1
        sendString = sendString & """Fol"":""" & Folder.CheckState & ""","
        'If Other is Checked Then send 1
        sendString = sendString & """Oth"":""" & Other.CheckState & ""","
        'send user id
        sendString = sendString & "#" & Login.getConnection.UserID
        sendString = sendString & "}"
        'Must end with new line in order to send to server
        sendString = sendString & vbNewLine
        My.Computer.FileSystem.WriteAllText("C:\test\testing.txt", sendString, True)

        'Send info to server
        If Login.getConnection IsNot Nothing AndAlso Login.getConnection.Client.Connected AndAlso Login.getConnection.Stream IsNot Nothing Then
            Dim buffer() As Byte = System.Text.Encoding.ASCII.GetBytes(sendString)
            Login.getConnection.Stream.Write(buffer, 0, buffer.Length)
        End If

        'Stops and starts timer
        Timer1.Stop()
        TimerLabel.Text = "00"
        Timer1.Start()

        'Creating border and pic arrays
        Dim Borders = {Border1, Border2, Border3, Border4, Border5, Border6, Border7, Border8}
        Dim Pics = {Pic1, Pic2, Pic3, Pic4, Pic5}

        'Fill picture boxes with most recent pictures
        For index = 0 To Pics.Length - 1
            Pics(index).Left = Pics(index).Left + 204
        Next

        Dim fileList As New List(Of FileInfo)
        fileList.AddRange(New DirectoryInfo("C:\test\HerbariumPictures\").GetFiles("*.jpg"))

        Dim newestFiles As System.Collections.Generic.IEnumerable(Of FileInfo) = From file In fileList Order By file.CreationTime Descending Take 1

        Dim fileInfoList As List(Of FileInfo) = newestFiles.ToList

        For index = 0 To Pics.Length - 1
            If Pics(index).Location = New Point(1813, 27) Then
                Pics(index).Location = New Point(793, 27)
                Pics(index).Load("C:\test\HerbariumPictures\" + fileInfoList.Item(0).Name)
            End If
        Next

        'Create a folder picture
        For index1 = 0 To Borders.Length - 1
            For index2 = 0 To Pics.Length - 1
                If Borders(index1).Location = New Point(559, 4) And Borders(index1).FillColor = Folder.BackColor And Pics(index2).Location = New Point(793, 27) Then
                    Pics(index2).Load("C:\test\folder\Folder.jpg")
                End If
            Next
        Next

        'Create an empty holding place
        For index1 = 0 To Borders.Length - 1
            For index2 = 0 To Pics.Length - 1
                If Borders(index1).Location = New Point(559, 4) And Borders(index1).FillColor = Empty.BackColor And Pics(index2).Location = New Point(793, 27) Then
                    Pics(index2).Load("C:\test\folder\Folder.jpg")
                End If
            Next
        Next

        ''After info is sent, clear textboxes
        'For Each i As TextBox In TextBoxArray
        '    i.Clear()
        '    If Not (i.Name = "m1p" Or i.Name = "m2v") Then
        '        i.TabStop = True
        '    End If
        'Next
        'BarCodeTextBox.Clear()
        'RecordEnteredBy.Text = Testing1.Login.LoginTextBox.Text

        'After info is sent, clear checkboxes
        Dim CheckBoxes = {MissingInformation, IllegibleInformation, Unreadable, Empty, Folder, Other}
        For index = 0 To CheckBoxes.Length - 1
            If CheckBoxes(index).CheckState = CheckState.Checked Then
                CheckBoxes(index).CheckState = CheckState.Unchecked
            End If
        Next


        'Move TextBoxes
        Dim TextBoxes = {Text1, Text2, Text3}
        For index = 0 To TextBoxes.Length - 1
            TextBoxes(index).Left = TextBoxes(index).Left + 204
        Next

        For index = 0 To TextBoxes.Length - 1
            If TextBoxes(index).Location = New Point(793, 27) Then
                TextBoxes(index).Location = New Point(181, 27)
            End If
        Next

        'Make sure first textbox says "Current Input"
        For index = 0 To TextBoxes.Length - 1
            If TextBoxes(index).Location = New Point(181, 27) Then
                TextBoxes(index).Text = "Input Info"
            End If
        Next

        'Move Borders
        For index = 0 To Borders.Length - 1
            Borders(index).Left = Borders(index).Left + 204
        Next

        For index = 0 To Borders.Length - 1
            If Borders(index).Location = New Point(1783, 4) Then
                Borders(index).Location = New Point(151, 4)
                Borders(index).FillColor = Color.Black
            End If
        Next

        'clear textboxes
        Barcode.Clear()
        VerbatimCollectionDate.Clear()
        dateLabel.Text = ""
        Town.Clear()
        County.Clear()
        Collector.Clear()
        'Move cursor back to barcode
        Barcode.Select()


    End Sub

    Private Sub CheckCompletedness()
        'If MissingInformation.Checked = False Then
        '    Dim TextBoxCheck As Array = {Genus, Species, Ir, i, Author, cc, RecordEnteredBy, State, County, Town, Collector, VerbatimCollectionDate}
        '    For Each i As TextBox In TextBoxCheck
        '        If i.Text = "" Then
        '            If Not i.Equals(Qualifier) Then
        '                sendButton.Enabled = False
        '                Exit Sub
        '            End If
        '        End If
        '    Next
        'End If
        sendButton.Enabled = True
    End Sub

    'Change the border color as tags checked
    Private Sub IllegibleInformation_CheckedChange(sender As Object, e As EventArgs) Handles IllegibleInformation.CheckedChanged
        Dim Borders = {Border1, Border2, Border3, Border4, Border5, Border6, Border7, Border8}
        For index = 0 To Borders.Length - 1
            If Borders(index).Location = New Point(151, 4) Then
                Borders(index).FillColor = IllegibleInformation.BackColor
            End If
        Next
    End Sub

    Private Sub Unreadable_CheckedChanged(sender As Object, e As EventArgs) Handles Unreadable.CheckedChanged

        Dim Borders = {Border1, Border2, Border3, Border4, Border5, Border6, Border7, Border8}
        For index = 0 To Borders.Length - 1
            If Borders(index).Location = New Point(151, 4) Then
                Borders(index).FillColor = Unreadable.BackColor
            End If
        Next
    End Sub

    Private Sub Folder_CheckedChanged(sender As Object, e As EventArgs) Handles Folder.CheckedChanged
        Dim Borders = {Border1, Border2, Border3, Border4, Border5, Border6, Border7, Border8}
        For index = 0 To Borders.Length - 1
            If Borders(index).Location = New Point(151, 4) Then
                Borders(index).FillColor = Folder.BackColor
            End If
        Next
        Dim texts = {Text1, Text2, Text3}
        If Folder.Checked = True Then
            For index = 0 To texts.Length - 1
                If texts(index).Location = New Point(181, 27) Then
                    texts(index).Text = "Folder"
                End If
            Next
        End If

        'After info is sent, clear textboxes
        For Each i As TextBox In TextBoxArray
            i.Clear()
            If Not (i.Name = "m1p" Or i.Name = "m2v") Then
                i.TabStop = True
            End If
        Next
        BarCodeTextBox.Clear()
        RecordEnteredBy.Text = Testing1.Login.LoginTextBox.Text

        Genus.Focus()

    End Sub
    'change colors of borders based on flag
    Private Sub MissingInformation_CheckedChanged(sender As Object, e As EventArgs) Handles MissingInformation.CheckedChanged
        Dim Borders = {Border1, Border2, Border3, Border4, Border5, Border6, Border7, Border8}
        For index = 0 To Borders.Length - 1
            If Borders(index).Location = New Point(151, 4) Then
                Borders(index).FillColor = MissingInformation.BackColor
            End If
        Next

        CheckCompletedness()
    End Sub

    Private Sub Empty_CheckedChanged(sender As Object, e As EventArgs) Handles Empty.CheckedChanged
        Dim Borders = {Border1, Border2, Border3, Border4, Border5, Border6, Border7, Border8}
        For index = 0 To Borders.Length - 1
            If Borders(index).Location = New Point(151, 4) Then
                Borders(index).FillColor = Empty.BackColor
            End If
        Next
        Dim texts = {Text1, Text2, Text3}
        If Empty.Checked = True Then
            For index = 0 To texts.Length - 1
                If texts(index).Location = New Point(181, 27) Then
                    texts(index).Text = "Empty Space"
                End If
            Next
        End If
    End Sub

    Private Sub UncertainDateCheckBox_CheckedChanged(sender As Object, e As EventArgs) Handles UncertainDateCheckBox.CheckedChanged
        Dim Borders = {Border1, Border2, Border3, Border4, Border5, Border6, Border7, Border8}
        For index = 0 To Borders.Length - 1
            If Borders(index).Location = New Point(151, 4) Then
                Borders(index).FillColor = UncertainDateCheckBox.BackColor
            End If
        Next
    End Sub

    Private Sub Other_CheckedChanged(sender As Object, e As EventArgs) Handles Other.CheckedChanged
        Dim Borders = {Border1, Border2, Border3, Border4, Border5, Border6, Border7, Border8}
        For index = 0 To Borders.Length - 1
            If Borders(index).Location = New Point(151, 4) Then
                Borders(index).FillColor = Other.BackColor
            End If
        Next
    End Sub

    Private Sub TextBox_TextChanged(sender As Object)
        If sender.Text = "{" Then
            sender.Text = "[TAB] to Cancel"
            BarCodeTextBox.Clear()
            BarCodeTextBox.Select()
            For Each i As TextBox In TextBoxArray
                i.Enabled = False
            Next
        End If
        CheckCompletedness()
    End Sub

    Private Sub ScientificName_TextChanged(sender As Object, e As EventArgs) Handles ScientificName.TextChanged
        TextBox_TextChanged(sender)
    End Sub

    Private Sub Genus_TextChanged(sender As Object, e As EventArgs) Handles Genus.TextChanged
        TextBox_TextChanged(sender)
    End Sub

    Private Sub Species_TextChanged(sender As Object, e As EventArgs) Handles Species.TextChanged
        TextBox_TextChanged(sender)
    End Sub

    Private Sub Ir_TextChanged(sender As Object, e As EventArgs) Handles Ir.TextChanged
        TextBox_TextChanged(sender)
    End Sub

    Private Sub i_TextChanged(sender As Object, e As EventArgs) Handles i.TextChanged
        TextBox_TextChanged(sender)
    End Sub

    Private Sub Author_TextChanged(sender As Object, e As EventArgs) Handles Author.TextChanged
        TextBox_TextChanged(sender)
    End Sub

    Private Sub cc_TextChanged(sender As Object, e As EventArgs) Handles cc.TextChanged
        TextBox_TextChanged(sender)
    End Sub

    Private Sub RecordEnteredBy_TextChanged(sender As Object, e As EventArgs) Handles RecordEnteredBy.TextChanged
        TextBox_TextChanged(sender)
    End Sub

    'make sure genus and species get put in scientific name
    Private Sub Species_Leave(sender As Object, e As EventArgs) Handles Species.Leave
        If ScientificName.Text = "" Then
            ScientificName.Text = Genus.Text & "" & Species.Text
        End If
    End Sub

    Private Sub State_Leave(sender As Object, e As EventArgs) Handles State.Leave
        Dim acs As New AutoCompleteStringCollection
        Dim count As Integer
        For Each i As String In states_of_towns
            If State.Text.ToLower = i.ToLower Then
                count += 1
            End If
        Next
        Dim NewNameString(count - 1) As String
        Dim index As Integer = 0
        For j = 0 To states_of_towns.Length - 1
            If states_of_towns(j).ToLower = State.Text.ToLower Then
                NewNameString(index) = towns(j)
                index += 1
            End If
        Next

        Town.AutoCompleteMode = AutoCompleteMode.SuggestAppend
        Town.AutoCompleteSource = AutoCompleteSource.CustomSource
        Town.AutoCompleteCustomSource = acs
        acs.AddRange(NewNameString)
    End Sub

    Private Sub State_TextChanged(sender As Object, e As EventArgs) Handles State.TextChanged
        TextBox_TextChanged(sender)
    End Sub


    Private Sub County_TextChanged(sender As Object, e As EventArgs) Handles County.TextChanged
        TextBox_TextChanged(sender)
    End Sub

    Private Sub Town_TextChanged(sender As Object, e As EventArgs) Handles Town.TextChanged
        TextBox_TextChanged(sender)
    End Sub

    Private Sub Collector_TextChanged(sender As Object, e As EventArgs) Handles Collector.TextChanged
        TextBox_TextChanged(sender)
    End Sub

    Private Sub Qualifier_TextChanged(sender As Object, e As EventArgs) Handles Qualifier.TextChanged
        TextBox_TextChanged(sender)
    End Sub

    Private Sub VerbatimCollectionDate_TextChanged(sender As Object, e As EventArgs) Handles VerbatimCollectionDate.TextChanged
        TextBox_TextChanged(sender)
    End Sub

    Private Sub Barcode_TextChanged(sender As Object, e As EventArgs) Handles Barcode.TextChanged
        TextBox_TextChanged(sender)
    End Sub

    'Create a timer
    Private Sub Timer1_Tick(sender As Object, e As EventArgs) Handles Timer1.Tick
        If TimerLabel.Text < 1000 Then
            TimerLabel.Text = Val(TimerLabel.Text) + 1
        Else
            Timer1.Stop()
            TimerLabel.Text = "Late"
        End If
    End Sub
    'move to empty textbox
    Private Sub MoveToNextEmptyTextbox()
        For Each i As TextBox In TextBoxArray
            If i.Text = "" Then
                i.Select()
                Exit For
            End If
        Next
    End Sub

    Private Sub InputForm_Disposed(sender As Object, e As EventArgs) Handles Me.Disposed
        Application.Exit()
    End Sub



    Private Sub BarCodeTextBox_Leave(sender As Object, e As EventArgs) Handles BarCodeTextBox.Leave
        For Each i As TextBox In TextBoxArray
            i.Clear()
        Next

        BarCodeTextBox.Text = "{" & BarCodeTextBox.Text

        Divided = BarCodeTextBox.Text.Split(",")
        Try
            For index = 0 To Divided.Length - 1
                Divided(index) = Divided(index).Replace("""", "")
                Divided(index) = Divided(index).Replace("{", "")
                Divided(index) = Divided(index).Replace("}", "")
                Dim Subdivided As Array
                Subdivided = Divided.GetValue(index).Split(":")

                Select Case Subdivided(0)
                    Case "m1p"
                        m1p.Text = Subdivided(1)
                    Case "m2v"
                        m2v.Text = Subdivided(1)
                    Case "g"
                        Genus.Text = Subdivided(1)
                    Case "s"
                        Species.Text = Subdivided(1)
                    Case "ir"
                        Ir.Text = Subdivided(1)
                    Case "i"
                        i.Text = Subdivided(1)
                    Case "a"
                        Author.Text = Subdivided(1)
                    Case "st"
                        State.Text = Subdivided(1)
                    Case "ct"
                        County.Text = Subdivided(1)
                    Case "cl"
                        Collector.Text = Subdivided(1)
                    Case "cc"
                        cc.Text = Subdivided(1)
                    Case "sn"
                        ScientificName.Text = Subdivided(1)


                End Select
            Next index
            For Each i As TextBox In TextBoxArray
                i.Enabled = True
                If i.Text <> "" Then
                    i.TabStop = False
                End If
            Next
        Catch ex As Exception

        End Try

        RecordEnteredBy.Text = Testing1.Login.LoginTextBox.Text()

        'fill in scientific name is genus and species filled in
        If ScientificName.Text = "" Then
            ScientificName.Text = Genus.Text & " " & Species.Text
        End If

        Barcode.Select()
    End Sub

    'Private Sub Barcode_Leave(sender As Object, e As EventArgs) Handles Barcode.Leave
    '    VerbatimCollectionDate.Select()
    'End Sub

    
    Private Sub LogOut_LinkClicked(sender As Object, e As LinkLabelLinkClickedEventArgs) Handles LogOut.LinkClicked
        Login.getConnection.Close()
        Testing1.Login.Show()
        Me.Hide()
    End Sub

    Private Sub VerbatimCollectionDate_Leave(sender As Object, e As EventArgs) Handles VerbatimCollectionDate.Leave
        day = ""
        month = ""
        year = ""
        Dim subStrings As String()
        Dim sDate As String = VerbatimCollectionDate.Text

        dateLabel.Text = ""

        If sDate <> "" Then

            sDate = sDate.ToLower
            sDate = OnlyAlphaNumericChars(sDate)
            subStrings = sDate.Split(" ")


            'Search for one and only one
            For index As Integer = 0 To subStrings.Length - 1
                If isYear(subStrings(index)) And Not isMonth(subStrings(index)) And Not isDay(subStrings(index)) Then
                    year = subStrings(index)
                    subStrings(index) = ""
                ElseIf Not isYear(subStrings(index)) And isMonth(subStrings(index)) And Not isDay(subStrings(index)) Then
                    month = subStrings(index)
                    subStrings(index) = ""
                ElseIf Not isYear(subStrings(index)) And Not isMonth(subStrings(index)) And isDay(subStrings(index)) Then
                    day = subStrings(index)
                    subStrings(index) = ""
                End If
            Next

            'Search for if other possibilities are already matched
            pullOutSingles(day, month, year, subStrings)

            'Order of presedence
            For index As Integer = 0 To subStrings.Length - 1
                If isYear(subStrings(index)) And year = "" Then
                    year = subStrings(index)
                    subStrings(index) = ""
                ElseIf isYear(subStrings(index)) And month = "" And isMonth(year) Then
                    month = year
                    year = subStrings(index)
                    subStrings(index) = ""
                ElseIf isYear(subStrings(index)) And isDay(year) And day = "" Then
                    day = year
                    year = subStrings(index)
                    subStrings(index) = ""
                ElseIf isMonth(subStrings(index)) And isDay(subStrings(index)) And month = "" And day = "" Then
                    month = subStrings(index)
                    subStrings(index) = ""
                End If
            Next

            'Search for if other possibilities are already matched
            pullOutSingles(day, month, year, subStrings)

            For index As Integer = 0 To subStrings.Length - 1
                If subStrings(index) <> "" Then
                    'ErrorText.Text &= vbNewLine & "Unused tokens"
                    IllegibleInformation.Checked = True
                End If
            Next

            'Remove unusable information
            If year = "" Then
                day = ""
                month = ""
            ElseIf month = "" Then
                day = ""
            End If

            'Convert to standard
            day = toDay(day)
            month = toMonth(month)
            year = toYear(year)

            'Display information
            dateLabel.Text = year & "-" & month & "-" & day
        End If

        'MoveToNextEmptyTextbox()
    End Sub

    Private Sub pullOutSingles(ByRef day As String, ByRef month As String, ByRef year As String, ByRef subStrings As String())
        For index As Integer = 0 To subStrings.Length - 1
            If isYear(subStrings(index)) And (Not isMonth(subStrings(index)) Or month <> "") And isDay(subStrings(index)) And year <> "" And day = "" Then
                day = subStrings(index)
                subStrings(index) = ""
            ElseIf isYear(subStrings(index)) And (Not isMonth(subStrings(index)) Or month <> "") And isDay(subStrings(index)) And year = "" And day <> "" Then
                year = subStrings(index)
                subStrings(index) = ""
            ElseIf isYear(subStrings(index)) And isMonth(subStrings(index)) And (Not isDay(subStrings(index)) Or day <> "") And year = "" And month <> "" Then
                year = subStrings(index)
                subStrings(index) = ""
            ElseIf isYear(subStrings(index)) And isMonth(subStrings(index)) And (Not isDay(subStrings(index)) Or day <> "") And year <> "" And month = "" Then
                month = subStrings(index)
                subStrings(index) = ""
            ElseIf (Not isYear(subStrings(index)) Or year <> "") And isMonth(subStrings(index)) And isDay(subStrings(index)) And month = "" And day <> "" Then
                month = subStrings(index)
                subStrings(index) = ""
            ElseIf (Not isYear(subStrings(index)) Or year <> "") And isMonth(subStrings(index)) And isDay(subStrings(index)) And month <> "" And day = "" Then
                day = subStrings(index)
                subStrings(index) = ""
            End If
        Next
    End Sub

    Private Function toYear(token As String) As String

        If IsNumeric(token) Then
            Dim number = CInt(token)
            If number < 100 Then
                If number <= (Now.Year - 2000) Then
                    If number < 10 Then
                        Return "200" & number
                    Else
                        Return "20" & number
                    End If
                Else
                    If number < 10 Then
                        Return "190" & number
                    Else
                        Return "19" & number
                    End If
                End If
            ElseIf number > Now.Year Then
                Return "> now error"
            End If
        End If

        Return token
    End Function

    Private Function toMonth(token As String) As String
        If Not IsNumeric(token) Then
            For m = 0 To months.Length - 1
                'Set month to this month
                If token.Equals(months(m)) Then
                    Return monthsNumeric.ElementAt(m)
                End If
            Next
        ElseIf IsNumeric(token) Then
            Dim number = CInt(token)
            If number < 10 Then
                Return ("0" & number)
            Else : Return number
            End If
        End If

        Return token
    End Function

    Private Function toDay(token As String) As String

        If IsNumeric(token) Then
            Dim number = CInt(token)
            If number < 10 Then
                Return "0" & number
            End If
        ElseIf Not IsNumeric(token) Then
            'Loop to find which month it is
            For d = 0 To days.Length - 1
                'Set month to this month
                If token.Equals(days(d)) Then
                    Return daysNumeric(d)
                End If
            Next
        End If

        Return token
    End Function

    Private Function isYear(token As String) As Boolean
        If IsNumeric(token) Then
            Dim number = CInt(token)
            If number < Now.Year Then
                If number > 99 And number < 1600 Then
                    'ErrorText.Text = vbNewLine & "Warning: range of year may be bad"
                    IllegibleInformation.Checked = True
                End If
                Return True
            Else
                'ErrorText.Text = vbNewLine & "Number greater than current year"
                IllegibleInformation.Checked = True
            End If
        End If
        Return False
    End Function

    Private Function isMonth(token As String) As Boolean

        If Not IsNumeric(token) Then
            'Loop to find which month it is
            For m = 0 To months.Length - 1
                'Set month to this month
                If token.Equals(months(m)) Then
                    Return True
                    Exit For
                End If
            Next
        ElseIf IsNumeric(token) Then
            Dim number = CInt(token)
            If number > 0 And number <= 12 Then
                Return True
            End If
        End If
        Return False

    End Function

    Private Function isDay(token As String) As Boolean
        If IsNumeric(token) Then
            Dim number = CInt(token)
            If number > 0 And number <= 31 Then
                Return True
            End If
        ElseIf Not IsNumeric(token) Then
            'Loop to find which month it is
            For d = 0 To days.Length - 1
                'Set month to this month
                If token.Equals(days(d)) Then
                    Return True
                    Exit For
                End If
            Next
        End If

        Return False
    End Function

    Public Function OnlyAlphaNumericChars(ByVal OrigString As _
String) As String
        '***********************************************************
        'INPUT:  Any String
        'OUTPUT: The Input String with all non-alphanumeric characters 
        '        removed
        'EXAMPLE Debug.Print OnlyAlphaNumericChars("Hello World!")
        'output = "HelloWorld")
        'NOTES:  Not optimized for speed and will run slow on long
        '        strings.  If you plan on using long strings, consider 
        '        using alternative method of appending to output string,
        '        such as the method at
        '***********************************************************
        Dim lLen As Long
        Dim sAns As String
        Dim lCtr As Long
        Dim sChar As String

        OrigString = Trim(OrigString)
        lLen = Len(OrigString)
        For lCtr = 1 To lLen
            sChar = Mid(OrigString, lCtr, 1)
            If IsAlphaNumeric(Mid(OrigString, lCtr, 1)) Then
                sAns = sAns & sChar
            Else
                sAns = sAns & " "
            End If
        Next
        OnlyAlphaNumericChars = sAns
    End Function

    Private Function IsAlphaNumeric(sChr As String) As Boolean
        IsAlphaNumeric = sChr Like "[0-9 A-Z a-z ]"
    End Function

    Shared Sub AppendOutput()

    End Sub



  
 

End Class